class CSet extends Set {
  union(...sets) {
    return CSet.union(this, ...sets);
  }

  intersection(...sets) {
    return CSet.intersection(this, ...sets);
  }

  difference(sets) {
    return CSet.difference(this, sets);
  }

  // 返回多个集合的并集
  static union(a, ...bSets) {
    const unionSet = new CSet(a);
    for (const b of bSets) {
      for (const bValue of b) {
        unionSet.add(bValue);
      }
    }
    return unionSet;
  }

  // 返回多个集合的交集
  static intersection(a, ...bSets) {
    const intersectionSet = new CSet(a);
    for (const aValue of intersectionSet) {
      for (const b of bSets) {
        if (!b.has(aValue)) intersectionSet.delete(aValue);
      }
    }
    return intersectionSet;
  }

  // 返回两个集合的差集
  static difference(a, b) {
    const differenceSet = new CSet(a);
    for (const bValue of b) {
      if (a.has(bValue)) {
        b.delete(bValue);
        differenceSet.delete(bValue);
      }
    }
    for (const item of b) {
      differenceSet.add(item);
    }
    return differenceSet;
  }
}

const a = new CSet(['0', '01', '03', '04']);
const bSet = new Set(['1', '2', '3', '4', '01']);

console.log(a.difference(bSet));

